{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f0755a32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['tom', 'bob']\n",
      "3\n",
      "2\n",
      "tom\n",
      "bob\n",
      "jane\n"
     ]
    }
   ],
   "source": [
    "class Company(object):\n",
    "    def __init__(self, employee_list):\n",
    "        self.employee = employee_list\n",
    "\n",
    "    def __getitem__(self, item):\n",
    "        return self.employee[item]\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.employee)\n",
    "\n",
    "company = Company([\"tom\", \"bob\", \"jane\"])\n",
    "\n",
    "# for em in company.employee:\n",
    "#     print(em)\n",
    "\n",
    "# 切片操作\n",
    "company1 = company[:2]      # 背后是使用的__getitem__魔法函数\n",
    "print(company1)\n",
    "\n",
    "# 使用len()函数\n",
    "print(len(company))         # 会报错：TypeError: object of type 'Company' has no len()\n",
    "                            # Company对象要想使用len()，则需要实现__len__魔方函数\n",
    "\n",
    "print(len(company1))        # 2。因为compamy1本身是list，所以可以使用len()\n",
    "\n",
    "\n",
    "# for遍历Company对象\n",
    "for em in company:\n",
    "    print(em)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c9c2fa8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tom.bob.jane\n"
     ]
    }
   ],
   "source": [
    "class Company(object):\n",
    "    def __init__(self, employee_list):\n",
    "        self.employee = employee_list\n",
    "    \n",
    "    def __str__(self):\n",
    "        return \".\".join(self.employee)\n",
    "    \n",
    "    def __repr__(self):\n",
    "        return \",\".join(self.employee)\n",
    "\n",
    "company = Company([\"tom\", \"bob\", \"jane\"])\n",
    "company # python解释器下才能执行，执行时会调用__repr__魔法函数\n",
    "print(company)  # 会调用str(),如果实现了__str__魔法函数，str()会调用__str__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c6d10749",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Nums(object):\n",
    "    def __init__(self, num):\n",
    "        self.num = num\n",
    "        \n",
    "    def __abs__(self):\n",
    "        return abs(self.num)\n",
    "\n",
    "my_num = Nums(-1)\n",
    "print(abs(my_num))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0939a1f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x:3, y:5\n"
     ]
    }
   ],
   "source": [
    "class MyVector(object):\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "    def __add__(self, other_instance):\n",
    "        re_vector = MyVector(self.x+other_instance.x, self.y+other_instance.y)\n",
    "        return re_vector\n",
    "    def __str__(self):\n",
    "        return \"x:{x}, y:{y}\".format(x=self.x, y=self.y)\n",
    "\n",
    "first_vec = MyVector(1,2)\n",
    "second_vec = MyVector(2,3)\n",
    "print(first_vec+second_vec)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
